package edu.gatech.cs2340.mule.model;
import java.io.Serializable;


/**
 * 
 * This class represents the Store in the town in a game of MULE.
 * In the store, the Player can buy and sell various resources as
 * well as MULEs.
 * 
 * @author Keegan Nesbitt
 *
 */
public class Store implements Serializable {
	private static final long serialVersionUID = 5832614012888170049L;
	
	private static final int ENERGY_PRICE = 25;
	private static final int SMITHORE_PRICE = 50;
	private static final int FOOD_PRICE = 30;
	private static final int FOOD_MULE_PRICE = 130;
	private static final int ENERGY_MULE_PRICE = 150;
	private static final int SMITHORE_MULE_PRICE = 175;
	
	private int energyAmount;
	private int foodAmount;
	private int smithoreAmount;
	private int muleAmount;
	
	/**
	 * Initializes the initial amounts of mules and resources in the store to
	 * the amounts specified by the beginner mode
	 * 
	 */
	public Store() {
		energyAmount = 16;
		smithoreAmount = 0;
		foodAmount = 16;
		muleAmount = 25;
	} // end Store constructor
	
	/**
	 * 
	 * @return The amount of energy the store has
	 */
	public int getEnergyStock() {
		return energyAmount;
	}
	
	/**
	 * 
	 * @return The amount of smithore in the store
	 */
	public int getSmithoreStock() {
		return smithoreAmount;
	}
	
	/**
	 * 
	 * @return The amount of food in the store
	 */
	public int getFoodStock() {
		return foodAmount;
	}
	
	/**
	 * 
	 * @param energyAmount The amount of energy to calculate the price for
	 * @return The price of the specified amount of energy
	 */
	public int calculateEnergyPurchasePrice(int energyAmount) {
		return energyAmount * ENERGY_PRICE;
	}
	
	/**
	 * 
	 * @param smithoreAmount The amount of smithore to calculate the price for
	 * @return The price of the specified amount of smithore
	 */
	public int calculateSmithorePurchasePrice(int smithoreAmount) {
		return smithoreAmount * SMITHORE_PRICE;
	}
	
	/**
	 * 
	 * @param foodAmount The amount of food to calculate the price for
	 * @return The price of the specified amount of food
	 */
	public int calculateFoodPurchasePrice(int foodAmount) {
		return foodAmount * FOOD_PRICE;
	}
	
	/**
	 * 
	 * @param muleType The type of mule for which to calculate the price for
	 * @return The price of the specified type of mule
	 */
	public int calculateMulePurchasePrice(String muleType) {
		if(muleType.equalsIgnoreCase(Mule.FOOD_TYPE)) {
			return  FOOD_MULE_PRICE;
		}
		else if(muleType.equalsIgnoreCase(Mule.ENERGY_TYPE)) {
			return ENERGY_MULE_PRICE;
		}
		else if(muleType.equalsIgnoreCase(Mule.SMITHORE_TYPE)) {
			return SMITHORE_MULE_PRICE;
		}
		else {
			throw new IllegalArgumentException(); 
		}
	}
	
	/**
	 * 
	 * Returns the Mule object that was bought and removes a mule from the store's inventory
	 * 
	 * @param type The type of mule being purchased
	 * @return The mule that was purchased from the store
	 */
	public Mule purchaseMule(String type) {
		Mule mule;
		
		if(type.equalsIgnoreCase(Mule.FOOD_TYPE)) {
			mule = new Mule(Mule.FOOD_TYPE);
		}
		else if(type.equalsIgnoreCase(Mule.ENERGY_TYPE)) {
			mule = new Mule(Mule.ENERGY_TYPE);
		}
		else if(type.equalsIgnoreCase(Mule.SMITHORE_TYPE)) {
			mule = new Mule(Mule.SMITHORE_TYPE);
		}
		else { 
			throw new IllegalArgumentException(); 
		}
		
		muleAmount--;
		return mule;
	}
	
	/**
	 * Checks to see if there are enough mules for purchase
	 * 
	 * @return True if a mule is in stock, false otherwise
	 */
	public boolean canPurchaseMule() {
		return muleAmount > 0;
	}
	
	/**
	 * 
	 * Checks to see if a specified amount of food can be purchased, and decrements
	 * the amount of food in the store if this is the case
	 * 
	 * @param foodAmount The amount of food being purchased from the store
	 * @return True if the specified amount of food can be purchased, false otherwise
	 */
	public boolean purchaseFood(int foodAmount) {
		if(this.foodAmount >= foodAmount) {
			this.foodAmount = this.foodAmount - foodAmount;
			return true;
		}
		else {
			return false;
		}
	}
	
	/**
	 * 
	 * Checks to see if a specified amount of energy can be purchased, and decrements
	 * the amount of energy in the store if this is the case
	 * 
	 * @param energyAmount The amount of energy being purchased from the store
	 * @return True if the specified amount of energy can be purchased, false otherwise
	 */
	public boolean purchaseEnergy(int energyAmount) {
		if(this.energyAmount >= energyAmount) {
			this.energyAmount = this.energyAmount - energyAmount;
			return true;
		}
		else {
			return false;
		}
	}
	
	/**
	 * 
	 * Checks to see if a specified amount of energy can be purchased, and decrements
	 * the amount of energy in the store if this is the case
	 * 
	 * @param smithoreAmount The amount of smithore being purchased from the store
	 * @return True if the specified amount of energy can be purchased, false otherwise
	 */
	public boolean purchaseSmithore(int smithoreAmount) {
		if(this.smithoreAmount >= smithoreAmount) {
			this.smithoreAmount = this.smithoreAmount - smithoreAmount;
			return true;
		}
		else {
			return false;
		}
	}
	
	/**
	 * Adds the specified amount of smithore to the store's inventory
	 * 
	 */
	public void tradeInSmithore(int smithoreAmount) {
		this.smithoreAmount = this.smithoreAmount + smithoreAmount;
	}
	
	/**
	 * Adds the specified amount of food to the store's inventory
	 * 
	 */
	public void tradeInFood(int foodAmount) {
		this.foodAmount = this.foodAmount + foodAmount;
	}
	
	/**
	 * Adds the specified amount of food to the store's inventory
	 *
	 */
	public void tradeInEnergy(int energyAmount) {
		this.energyAmount = this.energyAmount + energyAmount;
	}
} // end Store class
